package com.collect.cameroon.zhiScene.zhiWidget.zhiFilterDaily;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiScene.zhiWidget.zhiDialog.ZHIDialogItemClickListener;
import com.collect.cameroon.zhiScene.zhiWidget.zhiDialog.ZHIDialogNumberInput;
import com.collect.cameroon.zhiUtils.StringUtil;

import org.jaaksi.pickerview.dialog.DefaultPickerDialog;
import org.jaaksi.pickerview.picker.OptionPicker;
import org.jaaksi.pickerview.widget.DefaultCenterDecoration;
import org.jaaksi.pickerview.widget.PickerView;

import java.util.ArrayList;

public class ZHIRangeDaySelect extends RelativeLayout {

    private final Context mContext;

    private final String mMainText;

    private TextView tv_compare;

    private ImageView iv_compare_del;

    private TextView tv_overdue_days;

    private OptionPicker mPicker;

    private ArrayList<ZHIRangeDay> mRangeDayList;

    public ZHIRangeDaySelect(Context context) {
        this(context, null);
    }

    public ZHIRangeDaySelect(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIRangeDaySelect(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ZHIRangeDaySelect);
        mMainText = ta.getString(R.styleable.ZHIRangeDaySelect_mainText);
        ta.recycle();

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        View mView = View.inflate(mContext, R.layout.view_range_day_select, this);

        LinearLayout ll_compare = mView.findViewById(R.id.ll_compare);
        tv_overdue_days = mView.findViewById(R.id.tv_overdue_days);

        TextView tv_due_time_des = mView.findViewById(R.id.tv_due_time_des);
        tv_due_time_des.setText(mMainText);

        tv_compare = mView.findViewById(R.id.tv_compare);
        iv_compare_del = mView.findViewById(R.id.iv_compare_del);

        ll_compare.setOnClickListener(v -> showPickerDialog());

        tv_overdue_days.setOnClickListener(v -> {

            String title = mContext.getResources().getString(R.string.overdue_days);
            String content = getDays();
            String hintText = mContext.getResources().getString(R.string.hint_overdue_days);

            ZHIDialogNumberInput.showDialog(mContext, title, content, hintText, new ZHIDialogItemClickListener() {
                @Override
                public void onConfirmClick(String content) {
                    updateOverdueDays(content);
                }

                @Override
                public void onCancelClick() {

                }
            });
        });

        iv_compare_del.setOnClickListener(v -> {
            String startDate = StringUtil.safeString(tv_compare.getText().toString().trim());
            if (startDate.equalsIgnoreCase(mContext.getString(R.string.compare))) {
                showPickerDialog();
            } else {
                updateCompare(true, null);
            }
        });
    }

    private void initOptionPicker() {
        OptionPicker.OnOptionSelectListener mPickerSelectListener = (picker, selectedPosition, selectedOptions) -> {
            try {
                ZHIRangeDay zhiRangeDay = (ZHIRangeDay) selectedOptions[0];
                updateCompare(false, zhiRangeDay.getDayTitle());
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
        mPicker = new OptionPicker.Builder(mContext, 1, mPickerSelectListener)
                .setInterceptor((pickerView, params) -> {
                    pickerView.setVisibleItemCount(5);
                }).create();

        PickerView.sOutTextSize = 16;
        PickerView.sCenterTextSize = 18;
        PickerView.sCenterColor = mContext.getResources().getColor(R.color.theme_color_0);
        PickerView.sOutColor = Color.GRAY;
        PickerView.sDefaultVisibleItemCount = 3;

        DefaultCenterDecoration.sDefaultLineWidth = 1;
        DefaultCenterDecoration.sDefaultLineColor = mContext.getResources().getColor(R.color.theme_color_0);

        DefaultPickerDialog dialog = (DefaultPickerDialog) mPicker.dialog();
        dialog.getTitleView().setText(mContext.getString(R.string.pls_select));

        TextView btnCancel = (TextView) dialog.getBtnCancel();
        btnCancel.setText(mContext.getString(R.string.cancel));
        btnCancel.setVisibility(GONE);

        TextView btnConfirm = (TextView) dialog.getBtnConfirm();
        btnConfirm.setText(mContext.getString(R.string.confirm));

//        mPicker.setSelectedWithValues(provinceId, cityId, countyId);
//        mPicker.show();


        String[] rangeDays = new String[]{
                mContext.getResources().getString(R.string.more_than),
                mContext.getResources().getString(R.string.equal),
                mContext.getResources().getString(R.string.less_than)};

        mRangeDayList = new ArrayList<>();
        for (int i = 0; i < rangeDays.length; i++) {
            ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
            zhiRangeDay.setRangeId(i + "");
            zhiRangeDay.setDayTitle(rangeDays[i]);
            mRangeDayList.add(zhiRangeDay);
        }
        mPicker.setData(mRangeDayList);

    }

    private void updateCompare(boolean reset, String data) {
        try {
            if (reset) {
                tv_compare.setTextColor(mContext.getResources().getColor(R.color.theme_transparent_7));
                tv_compare.setText(mContext.getString(R.string.compare));
                iv_compare_del.setImageResource(R.drawable.ic_125);
                return;
            }

            if (TextUtils.isEmpty(data)) {
                return;
            }

            tv_compare.setText(data);
            tv_compare.setTextColor(mContext.getResources().getColor(R.color.theme_color_0));
            iv_compare_del.setImageResource(R.drawable.ic_124);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void updateOverdueDays(String days) {
        if (TextUtils.isEmpty(days)) {
            tv_overdue_days.setText(mContext.getResources().getString(R.string.hint_overdue_days));
            tv_overdue_days.setTextColor(mContext.getResources().getColor(R.color.theme_transparent_7));
            return;
        }

        tv_overdue_days.setText(days);
        tv_overdue_days.setTextColor(mContext.getResources().getColor(R.color.theme_color_0));

    }

    public void showPickerDialog() {
        try {
            if (mPicker == null) {
                initOptionPicker();
            }
            mPicker.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void reset() {
        updateCompare(true, null);
        updateOverdueDays(null);
    }

    public String getDays() {
        String tempValue = StringUtil.safeString(tv_overdue_days.getText().toString().trim());
        if (tempValue.equalsIgnoreCase(mContext.getResources().getString(R.string.hint_overdue_days))) {
            tempValue = "";
        }
        return tempValue;
    }

    public String getCompare() {
        String tempValue = StringUtil.safeString(tv_compare.getText().toString().trim());
        if (tempValue.equalsIgnoreCase(mContext.getResources().getString(R.string.compare))) {
            tempValue = "";
        }
        return tempValue;
    }
}
